import java.util.concurrent.TimeUnit;

/**
 * 解决死锁方案：破坏请求并持有条件
 */
public class UnDeadLock {
    public static void main(String[] args) {
        Object lockA=new Object();
        Object lockB=new Object();
        Thread t1=new Thread(()->{
            synchronized (lockA){
                System.out.println("线程1：得到了锁A");
                //业务代码
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                }
//                //尝试获取锁B
//                synchronized (lockB){
//                    System.out.println("线程1:得到了锁B");
//                    ///业务代码
//                    System.out.println("线程1:释放了锁B");
//                }
                System.out.println("线程1：释放了锁A");
            }
        },"线程1");
        t1.start();

        Thread t2=new Thread(()->{
            synchronized (lockB){
                System.out.println("线程2：得到了锁B");
                //业务代码
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                }
//                //尝试获取锁A
//                synchronized (lockA){
//                    System.out.println("线程2:得到了锁A");
//                    ///业务代码
//                    System.out.println("线程2:释放了锁A");
//                }
                System.out.println("线程2：释放了锁B");
            }
        },"线程2");
        t2.start();
    }
}
